<?php
/**
 * 用户处理model
 * Created by Forever.
 * User: Forever
 * Date: 2018/10/03
 * Time: 上午 10:39
 */
namespace app\api\model;
use think\Cache;
use \think\Model;
use \think\Db;
use EasyWeChat\Foundation\Application;
class User extends Base{

    public function initialize() { }

    public function getDetail($data=[]){
        $where = [
            "id"=>$data['id']
        ];
        $field = "id,name,addtime";
        $res =  Db::name(self::$tb['role'])->where($where)->field($field)->find();
        if($res){
            return $res;
        }else{
            return false;
        }
    }


    /*
    * 获取角色列表
    *@param data
    *@return array
    */
    public function getRoleList($data=[]){
        $where = [];
        if(isset($data["name"]) && !empty($data['name'])){
            $where['name'] = ["like","%{$data['name']}%"];
        }
        $list = Db::name(self::$tb['role'])->where($where)->select();
        return $list;
    }

   /*
    * 编辑角色
    *@param data
    *@return booth
    */
    public function editRole($data=[]){
        Db::startTrans();
        try{
            if(isset($data["id"]) && !empty($data['id'])){
                //编辑角色
                $update['name'] = $data['name'];
                $update['addtime'] = time();
                Db::name(self::$tb['role'])->where("id",$data['id'])->update($update);
            }else{
                //新增角色
                $insert['name'] = $data['name'];
                $insert['addtime'] = time();
                Db::name(self::$tb['role'])->insert($insert);
            }
           Db::commit();
           return true;
        } catch (\Exception $e) {
          Db::rollback();
          throw($e);
          return false;
        }
    }




    /*
    * 获取用户个人信息
    */
    public function getUserDetail($data=[]){
        $where["u.enable"] = 1;
        $where["u.ischeck"] = 1;
        switch($data['type']){
            case 1: $where['m.miniopenid'] = $data['openid'];break;  //小程序
            case 0: $where['m.mpopenid'] = $data['openid'];break;  //公众号
        }
        $field="u.id,u.name,u.phone,u.sale_total,u.flow_money,u.role_id,u.sale_total,u.shop_id,m.headimg";
        $info = Db::name(self::$tb['member'])->alias("m")->join("jun_user u","u.id=m.user_id")->where($where)->field($field)->find();
        if(!$info){
         return -10017;
        }
        $info['role'] = Db::name(self::$tb['role'])->where("id",$info['role_id'])->value("name");
        return $info;

    }


    /*
    * 编辑用户角色
    */
    public function editUserRole($data=[]){
        $code = cache($data['phone']);
        if(empty($result) || $result['last_time']<time()){
          return -10035;
        }
        if($code['code'] != $data['validate_code']){
           return -10036;
        }
        Db::startTrans();
        try{
            $update = [
                "phone"=>$data['phone'],
                "role_id"=>$data['role_id']
            ];
            $result = Db::name(self::$tb['user'])->where("id",$data['id'])->update($update);
            Db::commit();
           return true;
        } catch (\Exception $e) {
          Db::rollback();
          throw($e);
          return -10013;
        }
    }

}
